//
//  NemoRecordingStateView.m
//  NemoSDKDemo
//
//  Created by 杨旭东 on 2017/8/30.
//  Copyright © 2019 JackYang. All rights reserved.
//

#import "NemoRecordingStateView.h"


@interface NemoRecordingStateView()

@property (nonatomic, assign) int timeInterval;

@property (nonatomic, strong) NSTimer *timer;

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation NemoRecordingStateView

+ (instancetype)showNemoRecordingStateView{
    NemoRecordingStateView *recording = [[NSBundle mainBundle] loadNibNamed:@"NemoRecordingStateView" owner:nil options:nil].lastObject;
    return recording;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.frame = CGRectMake(5, 40, 60, 20);
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(5, 5, 10, 10);
    layer.cornerRadius = 5;
    layer.masksToBounds = YES;
    layer.backgroundColor = [UIColor redColor].CGColor;
    
    CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fade.fromValue = [NSNumber numberWithFloat:1.0];
    fade.toValue = [NSNumber numberWithFloat:0.0];
    fade.duration = 1.5f;
    fade.repeatCount = CGFLOAT_MAX;
    [layer addAnimation:fade forKey:@"opacity"];
    
    self.layer.cornerRadius = 3;
    self.layer.masksToBounds = YES;
    [self.layer addSublayer:layer];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
}

- (void)dismiss{
    if (_timer) {
        _timeInterval = 0;
        [_timer invalidate];
        _timer = nil;
    }
    
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeAllAnimations];
    }];
    [self removeFromSuperview];
}

- (void)updateRecordingDurtion{
    _timeInterval = _timeInterval + 1;
    
    int hour = 0, minute = 0, second = 0;
    if (_timeInterval >= 3600) {
        hour = _timeInterval / 3600;
    }
    minute = (_timeInterval % 3600) / 60;
    second = _timeInterval % 60;
    
    NSString *hint = @"录制中";
    if (hour != 0) {
        hint = [hint stringByAppendingString:[NSString stringWithFormat:@" %02d:%02d:%02d", hour, minute, second]];
        self.xy_width = 120;
    }else{
        hint = [hint stringByAppendingString:[NSString stringWithFormat:@" %02d:%02d", minute, second]];
    }
    self.label.text = hint;
    
    
    
}

- (NemoRecordingStateView *(^)(NemoRecordingType))recording{
    return ^(NemoRecordingType state){
        __weak NemoRecordingStateView *weakself = self;
        if (state < NemoRecordingTypeStopByOthers) {
            NSString *hint = @"录制中";
            
            if (state == NemoRecordingTypeBySelf) {
                hint = [hint stringByAppendingString:@" 00:00"];
                weakself.label.text = hint;
                weakself.xy_width = 100;
            }
            if (_timer == nil && state == NemoRecordingTypeBySelf) {
                _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateRecordingDurtion) userInfo:nil repeats:YES];
            }
            
        }else{
            _timeInterval = 0;
            [_timer invalidate];
            _timer = nil;
        }
        return self;
    };
}
@end
